// Figure 4

// Load Data
use "all_tasklevel_data.dta", clear

sort year_a month_a
egen time_group = group(month_a year_a) 
gen quarter_group = 0

local index=1
forvalues i = 1(3)39 {
	replace quarter_group = `i' if time_group >= `i' & time_group <= `i'+3
	local index = `index'+1
}

// Merge in survey information
merge m:1 worker_id using "survey_ids_gender.dta"
drop if _merge==2

g survey_match = (_merge==3)

g sex3 = sex
replace sex3 = sex_survey if missing(sex)

 drop if in_sample ==0
 drop if returned==1
 drop _merge

merge m:1 worker_id using "survey_data_all.dta"

g overlap = (time_diff < 0)

xtile length_group = total_length, n(4)
xtile age_group = age, n(4)

g health_bad = (health_work > 1)
labe var health_bad "Bad Health"

g ed_college = 0
replace ed_college = 1 if educ_c==1
replace ed_college = 1 if educ_mc==1
lab var ed_college "College +"

g econ_act_ = econ_activity
replace econ_act_ = 6 if econ_activity ==7
replace econ_act_ = 8 if econ_activity ==3

lab var sex3 "Female"

label define dec_ 1 "1st Decile Break" 2 "2nd Decile Break" 3 "3rd Decile Break" 4 "4th Decile Break" ///
  5 "5th Decile Break" 6 "6th Decile Break" 7 "7th Decile Break" 8 "8th Decile Break" 9 "9th Decile Break" 10 "10th Decile Break" 11 "First Task of Day", add
  label values abs_tdiff_group dec_

label define dec_2 1 "1st Decile Session Task" 2 "2nd Decile Session Task" 3 "3rd Decile Session Task" 4 "4th Decile Session Task" ///
  5 "5th Decile Session Task" 6 "6th Decile Session Task" 7 "7th Decile Session Task" 8 "8th Decile Session Task" 9 "9th Decile Session Task" 10 "10th Decile Session Task", add
 label values cluster10_group dec_2
 
label define len_ 1 "1st Quartile Length" 2 "2nd Quartile Length" 3 "3rd Quartile Length" 4 "4th Quartile Length" 
label values length_group len_

label var same_task "Same Task at t-1"

// Gelbach decompositions by children

foreach thing in task_type_ cluster10_group abs_tdiff_group ///
				same_task time_of_day quarter_group econ_act_ {
	qui tab `thing', g(indic_`thing')
	ren indic_`thing'1 _indic_`thing'1
}	

forvalues j = 0/1 {
b1x2 log_interval_wage if children==`j', x1all(sex3) ///
			x2all(log_task_reward indic_task_type* ///
		 log_worker_n indic_cluster10_group* ///
		 indic_abs_tdiff_group* overlap indic_time_of_day* indic_quarter_group*) x1only(sex3) ///
		 x2delta( pattern_group1 = indic_cluster10_group* : ///
				  pattern_group2 = indic_abs_tdiff_group* : ///
				  task_type_group = indic_task_type* : ///
				  exp_group = log_worker_n : ///
				  pattern_group4 = indic_time_of_day* : ///
				  pattern_group2a = overlap : ///
				  pattern_group3 = same_task : ///
				  task_reward_group = log_task_reward) ///
		 cluster(worker_id) nofull nobase

matrix tmp = e(Delta)
matrix tmp2 = e(Covdelta)
matrix decomp_b = J(1, 8, .)
matrix CI = J(2, 8, .)
forvalues i = 1/8 {
	matrix decomp_b[1, `i'] = tmp[1,`i']
	matrix CI[1, `i'] = tmp[1,`i'] - 1.96*sqrt(tmp2[`i',`i'])
	matrix CI[2, `i'] = tmp[1,`i'] + 1.96*sqrt(tmp2[`i',`i'])
}

coefplot(matrix(decomp_b)), ci(CI) xline(0)	xtitle("Contribution to Gender Pay Gap") ///
					 graphregion(color(white)) ylabel(1 "Work Session" ///
														2 "Breaks" ///
														3 "Task Type" ///
														4 "Total Experience" ///
														5 "Time of Day" ///
														6 "Overlap" ///
														7 "Same Task" ///
														8 "Task Reward") ///
														name(figure`j', replace) ///
														xscale(r(-.25 .25)) ///
														xlabel(-.2(.1).2)
																					
	}				


